﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KBEngineTool.Forms.Entity
{
    public partial class AppInfoWidget : UserControl
    {
        public AppInfoWidget()
        {
            InitializeComponent();

            this.Load += AppInfoWidget_Load;
        }

        private void AppInfoWidget_Load(object sender, EventArgs e)
        {
            KBEngine.Event.registerOut(KBEngine.MachineEvent.OnQueryAllInterfaces, this, nameof(__OnQueryAllInterfaces));
        }

        public void __OnQueryAllInterfaces()
        {
            m_DGV_allinfo.Rows.Clear();
            m_DGV_machinelist.Rows.Clear();

            var clientApp = Operation.Inst.ClientApp;
            foreach (var machineInfo in clientApp.MachineList)
            {
                int index = m_DGV_machinelist.Rows.Add();
                var newRow = m_DGV_machinelist.Rows[index];
                newRow.Cells[0].Value = machineInfo.intaddr.ToString();
                newRow.Cells[1].Value = string.Format("{0:F}", machineInfo.cpu);
                newRow.Cells[2].Value = string.Format("{0:F}%", machineInfo.mem);
                newRow.Cells[3].Value = string.Format("{0:F}", machineInfo.extradata2 / 100f * 1f);
                newRow.Cells[4].Value = string.Format("{0}m", machineInfo.usedmem / 1048576);
                newRow.Cells[5].Value = string.Format("{0}m/{1}m", machineInfo.extradata1 / 1048576, machineInfo.extradata / 1048576);

                foreach (var appInfo in clientApp.GetAppInfos(machineInfo.machineID))
                {
                    if (appInfo.componentType == KBEngine.Common.ComponentType.machine)
                        continue;
                    index = m_DGV_allinfo.Rows.Add();
                    newRow = m_DGV_allinfo.Rows[index];
                    newRow.Cells[0].Value = appInfo.intaddr.ToString();
                    newRow.Cells[1].Value = appInfo.fullname.ToString();
                    newRow.Cells[2].Value = appInfo.uid.ToString();
                    newRow.Cells[3].Value = appInfo.pid.ToString();
                    newRow.Cells[4].Value = appInfo.componentID.ToString();
                    newRow.Cells[5].Value = appInfo.globalOrderID.ToString();
                    newRow.Cells[6].Value = appInfo.genuuid_sections.ToString();
                    newRow.Cells[7].Value = string.Format("{0:F}%", appInfo.cpu);
                    newRow.Cells[8].Value = string.Format("{0:F}%", appInfo.mem);
                    newRow.Cells[9].Value = string.Format("{0}m", appInfo.usedmem / 1048576);
                    newRow.Cells[10].Value = appInfo.entities.ToString();
                    newRow.Cells[11].Value = appInfo.proxies.ToString();
                    newRow.Cells[12].Value = appInfo.clients.ToString();
                }
            }
        }
    }
}
